package com.ahua.mallchat.common.controller;

import com.ahua.mallchat.common.domain.RequestHolder;
import com.ahua.mallchat.common.domain.dto.chat.ChatMessageReq;
import com.ahua.mallchat.common.domain.vo.ApiResult;
import com.ahua.mallchat.common.domain.vo.ws.ChatMessageResp;
import com.ahua.mallchat.common.service.ChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 群聊相关接口
 */
@RestController
@RequestMapping("/capi/chat")
@Api(tags = "聊天室相关接口")
@Slf4j
public class ChatController {
    @Autowired
    private ChatService chatService;

    @PostMapping("/msg")
    @ApiOperation("发送消息")
    public ApiResult<ChatMessageResp> sendMsg(@Valid @RequestBody ChatMessageReq request) {
        Long msgId = chatService.sendMsg(request, RequestHolder.get().getUid());
        //返回完整消息格式，方便前端展示
        return ApiResult.success(chatService.getMsgResp(msgId, RequestHolder.get().getUid()));
    }

}

